//
//  TJVisitorView.swift
//  WeiBo
//
//  Created by TanJian on 15/12/5.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit
import SnapKit

class TJVisitorView: UIView {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //供外界调用，不同控制器的visitor界面接收不同图片和文字
    func setupVisitor(imgName:String?,text:String?){
        
        if let img = imgName,message = text{
            
            circleView.hidden = true
            houseView.image = UIImage(named: img)
            textLableView.text = message
            
        }else{
            
            startAnim()
            
        }
        
    }
    
    
    
    func startAnim (){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        
        //切换tabbar或者控制器，anim可以被释放
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
        
        
    }
    
    
    //懒加载界面的控件
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子图片
    private lazy var houseView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    //遮盖视图
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var textLableView:UILabel  = {
        
        let textLableView = UILabel()
        
        textLableView.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        textLableView.textColor = UIColor.darkGrayColor()
        textLableView.font = UIFont.systemFontOfSize(14)
        textLableView.numberOfLines = 0
        
        textLableView.textAlignment = .Center
        
        return textLableView
        
    }()
    
    lazy var registerButton:UIButton = {
       
        let registerButton = UIButton()
        
        registerButton.setTitle("注册", forState: .Normal)
        registerButton.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        //设置拉伸图片
        var image = UIImage(named: "common_button_white_disable")
        image = image?.stretchableImageWithLeftCapWidth(Int(image!.size.width)/2, topCapHeight: Int(image!.size.height)/2)
        registerButton.setBackgroundImage(image, forState: .Normal)

        registerButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return registerButton
        
    }()
    
    lazy var loginButton:UIButton = {
        
        let loginButton = UIButton()
        
        loginButton.setTitle("登陆", forState: .Normal)
        loginButton.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        loginButton.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        //设置拉伸图片
        var image = UIImage(named: "common_button_white_disable")
        image = image?.stretchableImageWithLeftCapWidth(Int(image!.size.width)/2, topCapHeight: Int(image!.size.height)/2)
        loginButton.setBackgroundImage(image, forState: .Normal)
        
        loginButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return loginButton
        
    }()


    
    private func setupUI(){
        
        self.backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(circleView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(textLableView)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //圆圈视图
        //给circleView添加约束
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        

//        
        //给houseView添加约束
        
        houseView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleView)
        }
        

        
        //cover的约束添加
        coverView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleView)
        }
        

        
        //lable添加约束
        textLableView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.centerX.equalTo(circleView)
            make.width.equalTo(224)
        }
        

        
        //registerButton添加约束
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLableView.snp_leading)
            make.top.equalTo(textLableView.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 35))
            
        }
        

        //loginButton添加约束
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(registerButton.snp_size)
            make.centerY.equalTo(registerButton.snp_centerY)
            make.trailing.equalTo(textLableView.snp_trailing)
        }
        

        
        
        
    }
  
}



